package com.flyoil.petromp.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cnpc.fyutils.EditText_util;
import com.flyoil.petromp.R;

/**
 * Created by songzhiiyn on 2017/10/13.
 * 自定义输入框有删除文本的功能
 */

public class EditTextDeleteView extends LinearLayout {
    private View view;
    private EditText edt_message;
    private ImageView img_delete;
    private Context context;
    private TextView tv_message;

    public EditTextDeleteView(Context context) {
        super(context);
        initview(context);
    }

    public EditTextDeleteView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initview(context);
    }

    public EditTextDeleteView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initview(context);
    }

    private void initview(Context context) {
        this.context = context;
        view = LayoutInflater.from(context).inflate(R.layout.view_edtview_delete_layout, this);
        edt_message = view.findViewById(R.id.edt_edtview_delete_message);
        EditText_util.seteidttext_astrictcharacter(edt_message);
        img_delete = view.findViewById(R.id.img_edtview_delete_icon);
        tv_message = view.findViewById(R.id.tv_edtview_delete_message);
        setImgdeleteOnclickListener();
        setEdtmessageFocusChange();
    }

    /**
     * 设置图片的点击事件
     */
    private void setImgdeleteOnclickListener() {
        img_delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                edt_message.setText("");
            }
        });
    }

    /**
     * 获取输入的内容
     *
     * @return
     */
    public String getText() {
        return edt_message.getText().toString().trim();
    }

    /**
     * 设置输入数据
     *
     * @param message
     */
    public void setText(String message) {
        edt_message.setText(message);
    }

    /**
     * 设置输入框的提示语
     *
     * @param hint
     */
    public void setHint(String hint) {
        edt_message.setHint(hint);
    }

    /**
     * 设置输入框焦点的监听
     */
    private void setEdtmessageFocusChange() {
        edt_message.setOnFocusChangeListener(new android.view.View.
                OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    // 此处为得到焦点时的处理内容
                    img_delete.setVisibility(VISIBLE);
                    img_delete.setClickable(true);
                } else {
                    // 此处为失去焦点时的处理内容
                    img_delete.setVisibility(GONE);
                    img_delete.setClickable(false);
                }
            }
        });
        edt_message.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                tv_message.setText(edt_message.getText().toString().trim());
                if (tv_message.getLineCount() > 1) {
                    edt_message.setGravity(Gravity.LEFT);
                } else {
                    edt_message.setGravity(Gravity.RIGHT);
                }
            }
        });
    }
}
